package com.jzdjt.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jzdjt.entity.ChargeEntity;
import com.jzdjt.tool.DBUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GetUserAmountServlet extends HttpServlet {

    public GetUserAmountServlet() {
        super();
    }

    @Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    	response.setContentType("text/html;charset=utf-8"); 
        response.setCharacterEncoding("UTF-8"); 
        boolean b=false;
        //根据标示名获取JSP文件中表单所包含的参数
        String stuid=request.getParameter("stuid");  
        String  result =  getBalance(stuid);
        PrintWriter out = response.getWriter();//回应请求
         
        out.write(result);
        out.flush();
        out.close();
        System.out.println(result); 
    }

    @Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException { 
    	 doGet(request, response); 
         //设置客户端的解码方式为utf-8
         response.setContentType("text/html;charset=utf-8"); 
         response.setCharacterEncoding("UTF-8"); 
         boolean b=false;
         //根据标示名获取JSP文件中表单所包含的参数
         String stuid=request.getParameter("stuid");  
         String  result =  getBalance(stuid);
         PrintWriter out = response.getWriter();//回应请求
          
         out.write(result);
         out.flush();
         out.close();
         System.out.println(result); 
    }
    
    private String getBalance(String stuid) { 
		try {
			String sql = "select balance from student where studentID='"+stuid+"'";
			System.out.println(sql);
	        DBUtil dbUtil = new DBUtil();
	        List<String>  balance = dbUtil.QueryFromAmount(sql);
	        if(balance==null||balance.size()==0){
	        	return "fdOWBNuU/sg=";
	        }
	        else if(balance.get(0)==null||balance.get(0).equals("")){
	        	return "fdOWBNuU/sg=";
	        }
	        else {
	        	return balance.get(0);
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
			return "fdOWBNuU/sg="; 
		} 
       // JSONArray array =  JSONArray.fromObject(balance);
       // return array.toString();
	}

}